package code.tools.commponents.button
{
	import code.structure.manager.sound.MySoundManager;
	import code.tools.commponents.Component;
	import code.tools.commponents.Style;
	import code.tools.commponents.TextBitmap;
	import code.tools.sound.src.SoundName;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * 图标按钮
	 * 设置 normal为默认样式
	 * 设置 hover 为鼠标悬停样式 (hover == null 时 悬停默认 加入发光效果)
	 * normal w * h
	 * @author liangqian
	 */
	public class MovieClipButton extends Component
	{
		private var _tipText:String = "";
//		private var tip:Tip;
		private var normal:int;
		private var hover:int;
		/**
		 * 按钮选中时的文字bmd
		 */
		private var select:int;
		private var _over:Boolean;
//		private var _down:Boolean;
		protected var _toggle:Boolean;
		protected var _selected:Boolean;
		/**
		 *  onClick(this);
		 */
		private var onClick:Function;

		private var mc:MovieClip;
		/**
		 * @param parent
		 * @param xpos
		 * @param ypos
		 * @param defaultHandler defaultHandler(this)
		 * @param tipText
		 */
		public function MovieClipButton(parent:DisplayObjectContainer ,
								  xpos:Number ,
								  ypos:Number ,
								  defaultHandler:Function ,
								  tipText:String = null)
		{
			super(parent , xpos , ypos);
			onClick = defaultHandler;
			_tipText = tipText;
//			tip = Tip.getInstance();
		}

		override protected function init():void
		{
			super.init();
			buttonMode = true;
			mouseChildren = false;
		}

		override protected function addChildren():void
		{

			addEventListener(MouseEvent.ROLL_OVER , overHandler);
			addEventListener(MouseEvent.MOUSE_DOWN , downHandler);
//			addEventListener(MouseEvent.CLICK , clickHandler);
		}

		override public function dispose(... rest):void
		{
			super.dispose();
			mouseChildren = mouseEnabled = false;
			onClick = null;
			removeEventListener(MouseEvent.MOUSE_UP , upHandler);
			removeEventListener(MouseEvent.ROLL_OVER , overHandler);
			removeEventListener(MouseEvent.MOUSE_DOWN , downHandler);
//			if(stage)
//				stage.removeEventListener(MouseEvent.MOUSE_UP , upHandler);
//			Effect.colorize(this , 0 , -1);
		}

		override public function draw():void
		{
			super.draw();
			if(mc == null)return;
			if (hover == -1)
			{
				mc.gotoAndStop(normal);
				filters = _over ? Style.HOVER_FILTERS : null;
			}
			else
				mc.gotoAndStop( _over ? hover : normal);
			if (_toggle)
				if (_selected)
					mc.gotoAndStop(select);
//			if (_over)
//				tip.show(this , _tipText);
//			else
//				tip.close(this);
		}

		private var _mc:MovieClip;
		/**
		 *设置BitmapData
		 * @param n 移出时
		 * @param h 移入时 若h为Null时显示发光效果，若不为空显示h
		 * @param s 选中时 有值时自动设为toggle
		 */
		public function setAppearance(m:MovieClip ,
									  nFrame:int,
									  hFrame:int = -1 ,
									  sFrame:int = -1
									 ):void
		{
//			if (!isSelf)
//				loadWuids.length = 0;
			_mc = m;
			this.mc = _mc;
			addChild(mc);
			if (mc)
				setSize(mc.width , mc.height);
			normal = nFrame;
			hover = hFrame;
			if (sFrame != -1)
			{
				_toggle = true;
				select = sFrame;
			}
			mc.gotoAndStop(nFrame);
			invalidate();
			dispatchEvent(new Event(Event.RESIZE));
		}

		private var _text:String    = "";

//		public function get text():String
//		{
//			return _text;
//		}

//		/**
//		 * 设置文字
//		 * @param text 要显示的文字
//		 * @param nc 未移入时的颜色
//		 * @param hc 移入时的颜色
//		 * @param sc 选中时的颜色 有值时自动设为toggle
//		 */
//		public function setText(text:String , nc:int , hc:int = -1 , sc:int = -1):void
//		{
//			_text = text;
//			icon.x = icon.y = 0;
//			normal = icon.drawButtonText(_text , nc);
//			if (hc != -1)
//				hover = icon.drawButtonText(_text , hc);
//			if (sc != -1)
//			{
//				_toggle = true;
//				select = icon.drawButtonText(_text , sc);
//			}
//			icon.bitmapData = normal;
//			if (normal)
//				setSize(normal.width , normal.height);
//			invalidate();
//		}

		/**
		 * 普通和悬停状态互换
		 */
//		public function exchangeAppearance():void
//		{
//			setAppearance(hover , normal);
//		}

		protected function overHandler(event:MouseEvent):void
		{
			_over = true;
			MySoundManager.playSound(SoundName.MOUSE_OVER, 0.2);
			invalidate();
			addEventListener(MouseEvent.ROLL_OUT , onMouseOut);
//			PlayerRoleSoundEffect.playBtnHover();
		}

		/**
		 * Internal mouseOut handler.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onMouseOut(event:MouseEvent):void
		{
			removeEventListener(MouseEvent.ROLL_OUT , onMouseOut);
			_over = false;
//			tip.close(this);
//			if (!_down)
			invalidate();
		}

		public var onMouseDown:Function;

		private function downHandler(e:MouseEvent):void
		{
			addEventListener(MouseEvent.MOUSE_UP , upHandler);
//			stage.addEventListener(MouseEvent.MOUSE_UP , upHandler);
//			_down = true;
			if (onMouseDown != null)
				onMouseDown(this);
		}

		protected function upHandler(e:MouseEvent):void
		{
			/*需要keeplive的组件 可能在按住状态切换*/
			removeEventListener(MouseEvent.MOUSE_UP , upHandler);
//			if(stage == null) return;
//			_down = false;
			if (_toggle)
				_selected = true;
//				_selected = !_selected;
			invalidate();
			onClick(this);
//			PlayerRoleSoundEffect.playBtnClick();
			e.stopImmediatePropagation();
		}

		public function set tipText(str:String):void
		{
			_tipText = str;
			invalidate();
		}

		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(value:Boolean):void
		{
			if (!_toggle)
				value = false;
			if (_selected == value)
				return;
			_selected = value;
			invalidate();
		}
	}
}
